package com.school.information.core.security.handler;

import com.alibaba.fastjson.JSON;
import com.school.information.enums.result.ResultStatusEnum;
import com.school.information.utils.ResultUtil;
import com.school.information.utils.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/*
 * 处理授权过程中出现的异常
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        String json = JSON.toJSONString(ResultUtil.error(ResultStatusEnum.FORBIDDEN));
        WebUtils.renderString(response, json);
    }
}
